#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include<cstring>
using namespace std;
const int N = 510;
int dp[N][N];
int a[N][N];
int n, m;
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }
    memset(dp, 0x3f3f3f3f, sizeof(dp));
    dp[0][1] = 0;
    dp[1][0] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + a[i][j];
        }
    }
    cout << dp[n][m] << endl;
    return 0;
}